package com.ruantu.clothing.service.report;

import com.ruantu.clothing.dao.RukuMapper;
import com.ruantu.clothing.dao.report.TailorReportMapper;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.Ruku;
import com.ruantu.clothing.entity.Tailor;
import com.ruantu.clothing.entity.report.TailorReport;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/8 0008.
 */
@Service
public class TailorReportServiceImpl implements TailorReportService {
    @Autowired
    private TailorReportMapper tailorReportMapper;
    @Autowired
    private RukuMapper rukuMapper;

    @Override
    public List<TailorReport> findListReport(TailorReport tailorReport, String sort, String order, PageBean pageBean, String startTime, String endTime, Integer fenlei) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(tailorReport.getProduct())) {
            map.put("product", tailorReport.getProduct());
        }
        if (StringUtils.isNotBlank(startTime)) {
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            map.put("endTime", endTime + " 23:59:59");
        }
        if (StringUtils.isNotBlank(tailorReport.getPinpai())) {
            map.put("pinpai", tailorReport.getPinpai());
        }
        if (StringUtils.isNotBlank(tailorReport.getSeason())) {
            map.put("season", tailorReport.getSeason());
        }
        if (StringUtils.isNotBlank(tailorReport.getYear())) {
            map.put("year", tailorReport.getYear());
        }
        if ("createTime".equals(sort)) {
            map.put("sort", "tailor.create_time");
        } else if ("caijianshuliang".equals(sort)) {
            map.put("sort", "sum(tailor_children.shuliang)");
        }
        map.put("order", order);
        map.put("fenlei", fenlei);
        //查询分页数量
        pageBean.setTotal(tailorReportMapper.findListCountReport(map));
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        return findStoreReport(map, fenlei);
    }

    @Override
    public List<TailorReport> findTailorSum(TailorReport tailorReport, String sort, String order, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(tailorReport.getProduct())) {
            map.put("product", tailorReport.getProduct());
        }
        if (StringUtils.isNotBlank(tailorReport.getPinpai())) {
            map.put("pinpai", tailorReport.getPinpai());
        }
        if (StringUtils.isNotBlank(tailorReport.getSeason())) {
            map.put("season", tailorReport.getSeason());
        }
        if (StringUtils.isNotBlank(tailorReport.getYear())) {
            map.put("year", tailorReport.getYear());
        }
        if (StringUtils.isNotBlank(startTime)) {
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            map.put("endTime", endTime + " 23:59:59");
        }
        int tailor = tailorReportMapper.findTailorSum(map);
        TailorReport ta = new TailorReport();
        ta.setCaijianshuliang(tailor);
        ta.setProduct("总计:");
        ta.setBaosun(-1);
        ta.setYiruku(-1);
        ta.setDairuku(-1);
        List<TailorReport> list = new ArrayList<>();
        list.add(ta);
        return list;
    }

    /**
     * 复用代码 查询裁剪数量
     *
     * @param map
     * @param fenlei
     * @return
     */
    public List<TailorReport> findStoreReport(Map<String, Object> map, Integer fenlei) {
        List<TailorReport> tailors = tailorReportMapper.findListReport(map);
        if (tailors.size() > 0) {
            StringBuffer tailorId = new StringBuffer();
            for (int i = 0; i < tailors.size(); i++) {
                if (i == tailors.size() - 1) {
                    tailorId.append("'" + tailors.get(i).getId() + "'");
                    break;
                }
                tailorId.append("'" + tailors.get(i).getId() + "',");
            }
            map.put("tailorId", tailorId.toString());
            List<TailorReport> list = tailorReportMapper.findRukushuByTailorIdReport(map);
            if (list.size() == 0) {
                return tailors;
            }
            StringBuffer danjuId = new StringBuffer();
            for (int j = 0; j < list.size(); j++) {
                if (j == list.size() - 1) {
                    danjuId.append("'" + list.get(j).getDanjuId() + "'");
                } else {
                    danjuId.append("'" + list.get(j).getDanjuId() + "',");
                }
            }
            map.put("danjuId", danjuId);
            //查询入库退货数量
            List<Ruku> tuihuo = rukuMapper.findTaiorTui(map);
            //如果没有退货
            if (tuihuo.size() != 0) {
                //如果有退货 把退货单的数据放到list.yiruku里面去
                for (int j = 0; j < list.size(); j++) {
                    for (int i = 0; i < tuihuo.size(); i++) {
                        if (tuihuo.get(i).getProduct().equals(list.get(j).getProduct())) {
                            if (fenlei == 0) {
                                list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                                break;
                            }
                            //当按颜色
                            if (fenlei == 1) {
                                if (list.get(j).getColor().equals(tuihuo.get(i).getColor())) {
                                    list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                                    break;
                                }
                            }
                            //当按尺码
                            if (fenlei == 2) {
                                if (list.get(j).getColor().equals(tuihuo.get(i).getColor())
                                        && list.get(j).getSize().equals(tuihuo.get(i).getSize())) {
                                    list.get(j).setYiruku(list.get(j).getYiruku() - Math.abs(tuihuo.get(i).getZongshuliang()));
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            for (int j = 0; j < list.size(); j++) {
                for (int i = 0; i < tailors.size(); i++) {
                    if (tailors.get(i).getId().equals(list.get(j).getId())
                            && tailors.get(i).getProduct().equals(list.get(j).getProduct())) {
                        //当款号
                        if (fenlei == 0) {
                            tailors.get(i).setYiruku(list.get(j).getYiruku());
                            break;
                        }
                        //当按颜色
                        if (fenlei == 1) {
                            if (tailors.get(i).getColor().equals(list.get(j).getColor())) {
                                tailors.get(i).setYiruku(list.get(j).getYiruku());
                                break;
                            }
                        }
                        //当按尺码
                        if (fenlei == 2) {
                            if (tailors.get(i).getColor().equals(list.get(j).getColor())
                                    && tailors.get(i).getSize().equals(list.get(j).getSize())) {
                                tailors.get(i).setYiruku(list.get(j).getYiruku());
                                break;
                            }
                        }
                    }
                }
            }
        }
        //根据裁剪单状态算出报损、待入库数量
        for (int i = 0; i < tailors.size(); i++) {
            if (tailors.get(i).getState() == 0) {
                tailors.get(i).setBaosun(0);
                tailors.get(i).setDairuku(tailors.get(i).getCaijianshuliang());
            } else if (tailors.get(i).getState() == 1) {
                tailors.get(i).setBaosun(0);
                tailors.get(i).setDairuku(tailors.get(i).getCaijianshuliang() - tailors.get(i).getYiruku());
            } else if (tailors.get(i).getState() == 2) {
                tailors.get(i).setBaosun(tailors.get(i).getCaijianshuliang() - tailors.get(i).getYiruku());
                tailors.get(i).setDairuku(0);
            }
        }
        return tailors;
    }


    @Override
    public XSSFWorkbook downloadExcel(TailorReport tailorReport, String startTime, String endTime, Integer fenlei) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(tailorReport.getProduct())) {
            map.put("product", tailorReport.getProduct());
        }
        if (StringUtils.isNotBlank(startTime)) {
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            map.put("endTime", endTime + " 23:59:59");
        }
        map.put("sort", "product");
        map.put("order", "asc");
        map.put("fenlei", fenlei);

        //查询需要导出的数据
        List<TailorReport> tailorReports = findStoreReport(map, fenlei);
        //Excel 表格
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        CellStyle center = wb.createCellStyle();
        center.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
        // 设置这些样式
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("商品款号");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(1);
        cell.setCellValue("商品名称");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(2);
        cell.setCellValue("铭牌价");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(3);
        cell.setCellValue("成本价");
        cell.setCellStyle(titleStyle);
        int count = 3;
        //分类为1或者2时按颜色
        if (fenlei == 1 || fenlei == 2) {
            cell = row.createCell(++count);
            cell.setCellValue("颜色");
            cell.setCellStyle(titleStyle);
        }
        //分类为2时按颜色
        if (fenlei == 2) {
            cell = row.createCell(++count);
            cell.setCellValue("尺码");
            cell.setCellStyle(titleStyle);
        }
        cell = row.createCell(++count);
        cell.setCellValue("裁剪数量");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("报损数");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("已入库数");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("待入库数");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("品牌");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("季节");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("年份");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("裁剪时间");
        cell.setCellStyle(titleStyle);
        row = sheet.createRow(1);
        cell = row.createCell(0);
        cell.setCellValue(startTime + "------" + endTime);
        cell.setCellStyle(center);
        for (int i = 0; i < tailorReports.size(); i++) {
            count = 3;
            row = sheet.createRow(i + 2);
            //款号
            cell = row.createCell(0);
            cell.setCellValue(tailorReports.get(i).getProduct());
            cell.setCellStyle(center);
            //商品名
            cell = row.createCell(1);
            cell.setCellValue(tailorReports.get(i).getProductName());
            cell.setCellStyle(center);
            //铭牌价
            cell = row.createCell(2);
            cell.setCellValue(tailorReports.get(i).getPrice());
            //成本价
            cell = row.createCell(3);
            cell.setCellValue(tailorReports.get(i).getPrice());

            if (fenlei == 1 || fenlei == 2) {
                //颜色
                cell = row.createCell(++count);
                cell.setCellValue(tailorReports.get(i).getColor());
                cell.setCellStyle(center);
            }
            //尺码
            if (fenlei == 2) {
                cell = row.createCell(++count);
                cell.setCellValue(tailorReports.get(i).getSize());
                cell.setCellStyle(center);
            }
            //裁剪数量
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getCaijianshuliang());
            //报损数量
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getBaosun());
            //已入库数
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getYiruku());
            //待入库数量
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getDairuku());
            //品牌
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getPinpai());
            cell.setCellStyle(center);
            //年份
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getYear());
            cell.setCellStyle(center);
            //季节
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getSeason());
            cell.setCellStyle(center);
            //裁剪时间
            cell = row.createCell(++count);
            cell.setCellValue(tailorReports.get(i).getCreateTime());
            cell.setCellStyle(center);
        }
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, count));
        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(count, 7000);
        sheet.setColumnWidth((count - 1), 4000);
        return wb;
    }

}
